<?php
/**
 * Index Controller
 *
 * @author      Hyper <hyper@flashbay.com>
 * @copyright   Copyright (c) 2011 (http://www.saleshero.com)
 */

class IndexController extends SalesHero_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        // Select data from mysql
        $sql = $this->db->select()->from('test')->order('id');
        $this->view->data = $this->db->fetchAll($sql);
    }

    public function testAction()
    {
        $request = $this->getRequest();
        if ($request->isPost()) {
            $params = $request->getParams();

            // insert data into mysql
            try {
                $content = trim($params['content']);
                if (empty($content)) {
                    $this->feedback(
                        array(
                            'title'     => 'Error occured',
                            'message'   => 'The content should not be empty',
                            'linktext'  => 'Go Back',
                        )
                    );
                }

                $this->db->insert('test', array('content' => $content));
            } catch (Exception $e) {
                die($e->getMessage());
            }

            // jump to index
            $this->getHelper('Redirector')->gotoUrl('/');
        }
    }

    public function deltestAction()
    {
        $id = $this->getRequest()->getParam('id');

        // delete data
        try {
            $where = $this->db->quoteInto('id = ?', $id);
            $this->db->delete('test', $where);
        } catch (Exception $e) {
            die($e->getMessage());
        }

        $this->getHelper('Redirector')->gotoUrl('/');
    }
}

